package com.ldu.controller;


import com.ldu.pojo.GoodsExtend;
import com.ldu.pojo.Image;
import com.ldu.pojo.Goods;
import com.ldu.pojo.User;
import com.ldu.pojo.Cart;
import com.ldu.service.GoodsService;
import com.ldu.service.CartService;
import com.ldu.service.UserService;
import com.ldu.service.ImageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="/user")
public class CartController {
	

	@Resource
	private GoodsService goodsService;

	@Resource
	private CartService cartService;

	@Resource
	private UserService userService;

	@Resource
	private ImageService imageService;

	ModelAndView mv = new ModelAndView();


	/**
	 * 我的购物车 查询出购物车中现有的商品id
	 *
	 * @return 返回的model为 goodsAndImage对象,该对象中包含goods 和 images，参考相应的类
	 */

	@RequestMapping(value = "/allCart")
	public String cart(HttpServletRequest request, Model model) {
		User cur_user = (User) request.getSession().getAttribute("cur_user");
		long userId = cur_user.getId();
		List<Cart> cartList = cartService.getCartByUserId(userId);
		model.addAttribute("cart", cartList);
		return "user/shopCar";
	}

	/**
	 * 添加商品进购物车
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/addCart/{id}")
	public String addCartItem(HttpServletRequest request , @PathVariable("id") Integer id) {
		User curUser = (User) request.getSession().getAttribute("cur_user");
		long userId = curUser.getId();
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setGoodsId((long) id);
		cartService.addCartByUserIdAndId(cart);
		return "redirect:/user/allCart";

	}
	/**
	 * 删除购物车中的商品
	 * @return
	 */
	@RequestMapping(value = "/deleteCart/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String deleteCart(HttpServletRequest request, @PathVariable("id") Long cartId) {
//		User cur_user = (User) request.getSession().getAttribute("cur_user");
//		Integer user_id = cur_user.getId();
		cartService.deleteCartByUserIdAndGoodsId(cartId);

		return "true";

	}

	/**
	 * 清空购物车中的商品
	 * @return
	 */
	@RequestMapping(value = "/clearCart")
	public String clearCart(HttpServletRequest request) {
		User curUser = (User) request.getSession().getAttribute("cur_user");
		long userId = curUser.getId();
		cartService.clearCart(userId);

		return "redirect:/user/shopCar";

	}

	@RequestMapping(value = "doSettlement/{cartId}")
	public String doSettlement(@PathVariable("cartId") Long cartId) {
		cartService.deleteCartByUserIdAndGoodsId(cartId);
		return "redirect:/goods/buyId/" + cartId;
	}


}
